package com.ropeok.Exception;



import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.InvalidClaimException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.ropeok.Result.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

//写自定义异常的第一步
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 全局异常捕获器捕获serviceimpl的throw出的异常,到被抛出的异常,再用Result包装
     * 捕获这个自定义异常包装成为一个自定义的Result对象
     * 如果抛出的的是ServiceException，则调用该方法
     * @param se 业务异常
     * @return Result
     */
    @ExceptionHandler(ServiceException.class)
    @ResponseBody
    public Result Handle(ServiceException se){
        return Result.error(se.getCode(), se.getMessage());
    }


    @ExceptionHandler(SignatureVerificationException.class)
    @ResponseBody
    public Result Handle(SignatureVerificationException se){
        return Result.error(se.getMessage()+"签名不一致");
    }

    @ExceptionHandler(TokenExpiredException.class)
    @ResponseBody
    public Result Handle(TokenExpiredException se){
        return Result.error(se.getMessage()+"令牌过期");
    }

    @ExceptionHandler(InvalidClaimException.class)
    @ResponseBody
    public Result Handle(InvalidClaimException se){
        return Result.error(se.getMessage()+"失效payload");
    }

    @ExceptionHandler(AlgorithmMismatchException.class)
    @ResponseBody
    public Result Handle(AlgorithmMismatchException se){
        return Result.error(se.getMessage()+"算法不匹配");
    }

    @ExceptionHandler
    @ResponseBody
    public Result Handle(Exception ex){
        return Result.error(ex.getMessage()+"系统异常");
    }

}
